//
//  MyCollectionViewController.m
//  com.modada.new
//
//  Created by ferver7 on 2019/3/31.
//  Copyright © 2019 ferver7. All rights reserved.
//

#import "MyCollectionViewController.h"
#import "WSFSlideTitlesView.h"
#import "AppBaseGoodItem.h"
#import "MyCollectionListCell.h"
#import "MDDGoodsDetailViewController.h"
#import "StoreDetailViewController.h"
@interface MyCollectionViewController ()<WSFSlideTitlesViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *selfTableView;
@property (nonatomic, strong) WSFSlideTitlesView *titlesTopView;
@property (nonatomic,strong) NSMutableArray *colItems;
@property (nonatomic,assign) NSInteger page;

@property (nonatomic,assign) NSInteger viewTypw; //0收藏商品 1收藏店铺
@end

@implementation MyCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self customNavigationItems];
    mNavitationTitle(@"我的收藏");
    [self.view addSubview:self.titlesTopView];
    self.selfTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if (self.viewTypw == 0) {
            [self loadMyCollectionGoodsList];
        }else{
            [self loadMyCollectionStoresList];
        }
    }];
    self.selfTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        if (self.viewTypw == 0) {
            [self loadMyCollectionGoodsList];
        }else{
            [self loadMyCollectionStoresList];
        }
    }];
    self.colItems = [[NSMutableArray alloc]init];
    self.page = 1;
    [self loadMyCollectionGoodsList];
}
#pragma mark -
#pragma mark - **************** tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.colItems.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyCollectionListCell * itemCell = mGetTableViewCell(@"myCollectionListCell");
    if (self.viewTypw == 0) {
        [itemCell reloadViewForModel:self.colItems[indexPath.section]];
    }else{
        NSDictionary * infoDic = self.colItems[indexPath.row];
        itemCell.nameLabel.text = infoDic[@"storeName"];
        [itemCell.logoImage sd_setImageWithURL:[NSURL URLWithString:infoDic[@"doorImage"]] placeholderImage:nil];
        itemCell.colNumsLabel.text = [NSString stringWithFormat:@"%@人收藏",infoDic[@"storeCollNum"]];
        if ([infoDic[@"storeType"] isEqualToString:@"1"]) {
            itemCell.priceLabel.text = @"个人店铺";
        }else{
            itemCell.priceLabel.text = @"商家店铺";
        }
    }
    return itemCell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 140;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.viewTypw == 0) {
        AppBaseGoodItem * itemModel = self.colItems[indexPath.row];
        MDDGoodsDetailViewController * detailVC = mGetStoryboardViewController(@"Main", @"mDDGoodsDetailViewController");
        detailVC.goodId = itemModel.gid;
        NavigationController_pushVC(detailVC);
    }else{
        NSDictionary * itemModel = self.colItems[indexPath.row];
        StoreDetailViewController * sdvc = mGetStoryboardViewController(@"Main", @"storeDetailViewController");
        sdvc.storeId = itemModel[@"storeid"];
        NavigationController_pushVC(sdvc);
    }
}
#pragma mark -
#pragma mark - **************** 数据请求
-(void)loadMyCollectionGoodsList
{
    if ([self.selfTableView.mj_header isRefreshing]) {
        self.page = 1;
    }
    if ([self.selfTableView.mj_footer isRefreshing]) {
        self.page ++;
    }
    NSMutableDictionary * parameters = [[NSMutableDictionary alloc]init];
    setDickeyobj(parameters, @"10", @"limit");
    NSString * pageStr = [NSString stringWithFormat:@"%ld",self.page];
    setDickeyobj(parameters, pageStr, @"page");
    [PPNetworkHelper POST:app_person_myCollectionGood parameters:parameters success:^(id responseObject) {
        [self.selfTableView.mj_header endRefreshing];
        [self.selfTableView.mj_footer endRefreshing];
        if (self.page == 1 && self.colItems.count > 0) {
            [self.colItems removeAllObjects];
        }
        if (YJPPSUCCESS(responseObject)) {
            for (NSDictionary * dataD in responseObject[@"data"][@"list"]) {
                AppBaseGoodItem * item = [[AppBaseGoodItem alloc]init];
                [item setValuesForKeysWithDictionary:dataD];
                [self.colItems addObject:item];
            }
            if ([responseObject[@"data"][@"list"] count] < 10) {
                [self.selfTableView.mj_footer endRefreshingWithNoMoreData];
                
            }
        }else{
            
        }
        [self.selfTableView reloadData];
    } failure:^(NSError *error) {
        [self.selfTableView.mj_header endRefreshing];
        [self.selfTableView.mj_footer endRefreshing];
    } showLoading:nil];
}
#pragma mark -
#pragma mark - **************** 数据请求
-(void)loadMyCollectionStoresList
{
    if ([self.selfTableView.mj_header isRefreshing]) {
        self.page = 1;
    }
    if ([self.selfTableView.mj_footer isRefreshing]) {
        self.page ++;
    }
    NSMutableDictionary * parameters = [[NSMutableDictionary alloc]init];
    setDickeyobj(parameters, @"10", @"limit");
    NSString * pageStr = [NSString stringWithFormat:@"%ld",self.page];
    setDickeyobj(parameters, pageStr, @"page");
    [PPNetworkHelper POST:app_person_myCollectionStore parameters:parameters success:^(id responseObject) {
        [self.selfTableView.mj_header endRefreshing];
        [self.selfTableView.mj_footer endRefreshing];
        if (self.page == 1 && self.colItems.count > 0) {
            [self.colItems removeAllObjects];
        }
        if (YJPPSUCCESS(responseObject)) {
            for (NSDictionary * dataD in responseObject[@"data"][@"list"]) {
                [self.colItems addObject:dataD];
            }
            if ([responseObject[@"data"][@"list"] count] < 10) {
                [self.selfTableView.mj_footer endRefreshingWithNoMoreData];
            }
        }else{
            
        }
        [self.selfTableView reloadData];
    } failure:^(NSError *error) {
        [self.selfTableView.mj_header endRefreshing];
        [self.selfTableView.mj_footer endRefreshing];
    } showLoading:nil];
}
#pragma makr -lozyLoad
-(WSFSlideTitlesView *)titlesTopView{
    if (!_titlesTopView) {
        WSFSlideTitlesViewSetting *titlesSetting = [[WSFSlideTitlesViewSetting alloc] init];
        titlesSetting.titlesArr = @[ @"商品",@"店铺"];
        titlesSetting.frame = CGRectMake(0, getRectNavAndStatusHight, mScreenWidth, 44);
        titlesSetting.lineHeight = 1.5;
        titlesSetting.lineHidden = NO;
        titlesSetting.spaceLineHidden = YES;
        titlesSetting.lineWidth = 50;
        titlesSetting.textColor = [UIColor AxcUI_colorWithHexCode:@"#333333"];
        titlesSetting.selectedTextColor = [UIColor AxcUI_colorWithHexCode:@"#FC8963"];
        titlesSetting.lineBottomSpace = 0;
        titlesSetting.lineColor = [UIColor AxcUI_colorWithHexCode:@"#FC8963"];;
        //        titlesSetting.showRedPoint = YES;
        _titlesTopView = [[WSFSlideTitlesView alloc] initWithSetting:titlesSetting];
        _titlesTopView.delegate = self;
    }
    return _titlesTopView;
}
- (void)slideTitlesView:(WSFSlideTitlesView *)titlesView didSelectButton:(UIButton *)button atIndex:(NSUInteger)index{
    self.viewTypw = index;
    [self.selfTableView.mj_header beginRefreshing];
}
@end
